package net.siufung.market.base.permit.controller.regulation;

import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.permit.controller.model.dto.MenuUpdateDto;
import net.siufung.market.base.permit.mapper.IMenuMapper;

/**
 * @author 陈建峰
 * @since 2022/8/10 2:15 下午
 */
@AllArgsConstructor
public class MenuUpdateRegulation extends AbstractRegulation<MenuUpdateDto> {

    private static final long serialVersionUID = -6598449872240872667L;

    private final IMenuMapper menuMapper;

    @Override
    public boolean isSatisfiedBy(MenuUpdateDto menuUpdateDto) {
        VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
        verifyRepeatDto.setMethodEventEnum(MethodEventEnum.UPDATE);
        verifyRepeatDto.setPk(menuUpdateDto.getId());
        verifyRepeatDto.setVerifyKeyValue(menuUpdateDto.getMenuName());
        if(!menuMapper.checkMenuNameRepeat(verifyRepeatDto)){
            throw new BizServiceException("菜单名称重复！");
        }
        verifyRepeatDto.setVerifyKeyValue(menuUpdateDto.getRouter());
        if(!menuMapper.checkMenuRouterRepeat(verifyRepeatDto)){
            throw new BizServiceException("菜单路由重复！");
        }
        return Boolean.TRUE;
    }
}
